package com.joesem.sale.client.db

import android.content.Context
import com.joesem.sale.client.MyApplication


class DB private constructor() {
    private val sharedPreferences =
        MyApplication.instance.getSharedPreferences("Cache_User", Context.MODE_PRIVATE)

    companion object {
        val db by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { DB() }
    }

    fun saveString(key: String, data: String?) {
        sharedPreferences.edit()
            .putString(key, data.orEmpty())
            .apply()

    }

    fun getString(key: String): String? {
        return sharedPreferences.getString(key, "")
    }

    fun saveInt(key: String, data: Int?) {
        sharedPreferences.edit()
            .putInt(key, data ?: -1)
            .apply()
    }

    fun getInt(key: String): Int {
        return sharedPreferences.getInt(key, -1)
    }

    fun saveBoolean(key: String, data: Boolean?) {
        sharedPreferences.edit()
            .putBoolean(key, data ?: false)
            .apply()
    }

    fun getBoolean(key: String): Boolean {
        return sharedPreferences.getBoolean(key, false)
    }

    fun saveDouble(key: String, data: Double?) {
        sharedPreferences.edit()
            .putString(key, (data ?: "0.0").toString())
            .apply()
    }

    fun getDouble(key: String): Double {
        return sharedPreferences.getString(key, "0.0")?.toDoubleOrNull() ?: 0.0
    }

    fun clear(){
        sharedPreferences.edit().clear().apply()
    }

}